package com.tomorrow.runner.activity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.tomorrow.runner.R;
import com.tomorrow.runner.activity.basic.BasicActivity;
import com.tomorrow.runner.activity.main.HomeActivity;
import com.tomorrow.runner.data.bean.UserBean;
import com.tomorrow.runner.service.Result;
import com.tomorrow.runner.widget.UserForm;

import java.util.Calendar;

import cn.sucun.android.utils.DateUtils;

/**
 * 登陆界面
 * Created by tomorrow on 16-4-20.
 */
public class LoginActivity extends BasicActivity implements View.OnClickListener, UserForm.UserFormVisibleChangedListener {

    private TextView mBtnRegister;
    private Button mBtnSubmit;
    private Button mBtnCancel;

    private UserForm mUserForm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        mBtnSubmit = (Button) findViewById(R.id.btn_submit);
        mBtnRegister = (TextView) findViewById(R.id.btn_register);
        mBtnCancel = (Button) findViewById(R.id.btn_cancel_register);

        mBtnSubmit.setOnClickListener(this);
        mBtnRegister.setOnClickListener(this);
        mBtnCancel.setOnClickListener(this);

        mUserForm = new UserForm(this,UserForm.MODEL_LOGIN);
        mUserForm.setFormVisibleListener(this);
        mUserForm.setValidationListener(mValidationListener);
    }

    Validator.ValidationListener mValidationListener = new Validator.ValidationListener() {
        @Override
        public void onValidationSucceeded() {
            switch (mUserForm.getModel()){
                case UserForm.MODEL_LOGIN:
                    doSubmitLogin();
                    break;

                case UserForm.MODEL_REGISTER:
                    doSubmitRegister();
                    break;
            }
        }

        @Override
        public void onValidationFailed(View failedView, Rule<?> failedRule) {
            if (failedView instanceof EditText) {
                ((EditText) failedView).setError(failedRule.getFailureMessage());
            } else {
                showMsgToast(failedRule.getFailureMessage());
            }
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                mUserForm.validate();
                break;
            case R.id.btn_register:
                mUserForm.setModel(UserForm.MODEL_REGISTER);
                break;

            case R.id.btn_cancel_register:
                mUserForm.setModel(UserForm.MODEL_LOGIN);
                break;
            case R.id.birthday_label:
                int year = Calendar.getInstance().get(Calendar.YEAR);
                int month = Calendar.getInstance().get(Calendar.MONTH);
                int date = Calendar.getInstance().get(Calendar.DATE);
                DatePickerDialog datePickerDialog = new DatePickerDialog(this,
                        new DatePickerDialog.OnDateSetListener() {
                            @Override
                            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                                mUserForm.mBirthday.setText(getString(R.string.default_date,year,monthOfYear,dayOfMonth));
                            }
                        },year,month,date);
                datePickerDialog.show();
                break;
        }
    }

    private void doSubmitLogin() {
        String username = mUserForm.mEditUsername.getText().toString();
        String pwd = mUserForm.mEditPwd.getText().toString();
        Result result = mAccountService.login(username, pwd);
        Throwable error = result.getError();
        if (error == null){
            Intent intent = new Intent(LoginActivity.this,HomeActivity.class);
            startActivity(intent);
            finish();
        }
        else{
            showMsgToast(error.getMessage());
        }
    }

    private void doSubmitRegister() {
        String username = mUserForm.mEditUsername.getText().toString();
        String pwd = mUserForm.mEditPwd.getText().toString();
        String nickName = mUserForm.mEditNickname.getText().toString();
        boolean gender = mUserForm.mRadioMale.isChecked();
        long birthday = DateUtils.formatStringDate(mUserForm.mBirthday.getText().toString(),false).getTime();
        double weight = Double.parseDouble(mUserForm.mEditWeight.getText().toString());
        double tall = Double.parseDouble(mUserForm.mEditTall.getText().toString());


        UserBean userBean = new UserBean();
        userBean.username = username;
        userBean.pwd = pwd;
        userBean.nickname = nickName;
        userBean.birthday = birthday;
        userBean.gender = gender;
        userBean.weight = weight;
        userBean.tall = tall;
        Result result = mAccountService.register(userBean);
        Throwable error = result.getError();
        if (error == null){
            mUserForm.setModel(UserForm.MODEL_LOGIN);
            showMsgToast("注册成功");
        }
        else{
            showMsgToast(error.getMessage());
        }
    }

    @Override
    public void onPanelVisibleChanged(int mode, boolean visible) {
        if (visible){
            mBtnRegister.setVisibility(View.GONE);
            mBtnCancel.setVisibility(View.VISIBLE);
            mBtnSubmit.setText(getText(R.string.confirm));
        }
        else{
            mBtnRegister.setVisibility(View.VISIBLE);
            mBtnCancel.setVisibility(View.GONE);
            mBtnSubmit.setText(getText(R.string.txt_login));
        }
    }
}
